package com.example.lianxi222.fragment.home;

import androidx.lifecycle.MutableLiveData;

import com.example.mvvm_base.base.model.BaseRepo;
import com.example.mvvm_base.userbean.BannerEntity;
import com.example.mvvm_base.userbean.LikeEntity;
import com.example.mvvm_base.userbean.VideoEntity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomeRepo extends BaseRepo {
    public void getBannerData(MutableLiveData<BannerEntity> success,MutableLiveData<String> failure){
        apiService.getBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BannerEntity bannerEntity) {
                        success.setValue(bannerEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                       failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void getVideoData(int page, int pageSize, MutableLiveData<VideoEntity> success,MutableLiveData<String> failure){
        apiService.getVideo(page,pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoEntity videoEntity) {
                      success.setValue(videoEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                      failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void like(int id, MutableLiveData<LikeEntity> success,MutableLiveData<String> failure){
        apiService.like(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LikeEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LikeEntity likeEntity) {
                        success.setValue(likeEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                       failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void unLike(int id, MutableLiveData<LikeEntity> success,MutableLiveData<String> failure){
        apiService.unLike(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LikeEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LikeEntity likeEntity) {
                         success.setValue(likeEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                       failure.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
